home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr48 / stk100.zip / PLAYDWD.C < prev    next >
C/C++ Source or Header  |  1995-02-21  |  11KB  |  414 lines

  1. /******************************************************************************
  2. File:          playdwd.c
  3. Tab stops: every 2 collumns
  4. Project:     DWD Player
  5. Copyright: 1994 DiamondWare, Ltd.  All rights reserved.*
  6. Written:     Keith Weiner & Erik Lorenzen
  7. Purpose:     Contains simple example code to show how to load/play a .DWD file
  8. History:     KW 10/21/94 Started
  9.                      EL 02/21/95 Finalized
  10.  
  11. Notes
  12. -----
  13.  
  14. The bulk of this file is error checking logic.
  15.  
  16. However, this code isn't really robust when it comes to standard error checking
  17. and particularly recovery, software engineering technique, etc.  A buffer
  18. is statically allocated.    A better technique would be to use fstat() or stat()
  19. to determine the file's size then malloc(size).  The STK will handle songs
  20. larger than 64K (but not digitized sounds).  Obviously, you'd need to fread()
  21. such a file in chunks, or write some sort of hfread() (huge fread).  Also,
  22. exitting and cleanup is not handled robustly in this code.    The code below can
  23. only be validated by extremely careful scrutiny to make sure each case is
  24. handled properly.  A better method would the use of C's atexit function.
  25.  
  26. But all such code would make this example file less clear; its purpose was
  27. to illustrate how to call the STK, not how to write QA-proof software.
  28.  
  29.  
  30. *Permission is expressely granted to use DisplayError or any derivitive made
  31.  from it to registered users of the STK.
  32. ******************************************************************************/
  33.  
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <conio.h>
  39.  
  40. #include "dws.h"
  41.  
  42.  
  43. #define BUFFSIZE 65535        //If the linker outputs the error
  44.                                                     //"stack plus data exceed 64K"
  45.                                                     //try reducing BUFFSIZE to about 32K,
  46.                                                     //or compile for large model.
  47. byte sound[BUFFSIZE];
  48.  
  49.  
  50.  
  51. static void DisplayError(word errornum)
  52. {
  53.     switch (errornum)
  54.     {
  55.         case dws_EZERO:
  56.         {
  57.             /*
  58.              . This should not have happened, considering how we got here!
  59.             */
  60.             printf("I'm confused!  Where am I?  HOW DID I GET HERE????\n");
  61.             printf("The ERROR number is: %d\n",errornum);
  62.  
  63.             break;
  64.         }
  65.         case dws_NOTINITTED:
  66.         {
  67.             /*
  68.              . If we get here, it means you haven't called dws_Init().
  69.              . The STK needs to initialize itself and the hardware before
  70.              . it can do anything.
  71.             */
  72.             printf("The STK was not initialized\n");
  73.  
  74.             break;
  75.         }
  76.         case dws_ALREADYINITTED:
  77.         {
  78.             /*
  79.              . If we get here, it means you've called dws_Init() already.  Calling
  80.              . dws_DetectHardWare() at this point would cause zillions of
  81.              . problems if we let the call through.
  82.             */
  83.             printf("The STK was already initialized\n");
  84.  
  85.             break;
  86.         }
  87.         case dws_NOTSUPPORTED:
  88.         {
  89.             /*
  90.              . If we get here, it means that either the user's machine does not
  91.              . support the function you just called, or the STK was told not to
  92.              . support it in dws_Init.
  93.             */
  94.             printf("Function not supported\n");
  95.  
  96.             break;
  97.         }
  98.         case dws_DetectHardware_UNSTABLESYSTEM:
  99.         {
  100.             /*
  101.              . Please report it to DiamondWare if you get here!
  102.              .
  103.              . Ideally, you would disable control-C here, so that the user can't
  104.              . hit control-alt-delete, causing SmartDrive to flush its (possibly
  105.              . currupt) buffers.
  106.             */
  107.             printf("The system is unstable!\n");
  108.             printf("Please power down now!");
  109.  
  110.             #define ever ;;
  111.             for (ever);
  112.         }
  113.  
  114.         /*
  115.          . The following three errors are USER/PROGRAMMER errors.  You forgot
  116.          . to fill the cardtyp struct full of -1's (except in those fields
  117.          . you intended to override, or the user (upon the unlikly event that
  118.          . the STK was unable to find a card) gave you a bad overide value.
  119.         */
  120.         case dws_DetectHardware_BADBASEPORT:
  121.         {
  122.             /*
  123.              . You set dov.baseport to a bad value, or
  124.              . didn't fill it with a -1.
  125.             */
  126.             printf("Bad port address\n");
  127.  
  128.             break;
  129.         }
  130.         case dws_DetectHardware_BADDMA:
  131.         {
  132.             /*
  133.              . You set dov.digdma to a bad value, or
  134.              . didn't fill it with a -1.
  135.             */
  136.             printf("Bad DMA channel\n");
  137.  
  138.             break;
  139.         }
  140.         case dws_DetectHardware_BADIRQ:
  141.         {
  142.             /*
  143.              . You set dov.digirq to a bad value, or
  144.              . didn't fill it with a -1.
  145.             */
  146.             printf("Bad IRQ level\n");
  147.  
  148.             break;
  149.         }
  150.         case dws_Kill_CANTUNHOOKISR:
  151.         {
  152.             /*
  153.              . The STK points the interrupt vector for the sound card's IRQ
  154.              . to its own code in dws_Init.
  155.              .
  156.              . dws_Kill was unable to restore the vector to its original
  157.              . value because other code has hooked it after the STK
  158.              . initialized(!)  This is really bad.    Make the user get rid
  159.              . of it and call dws_Kill again.
  160.             */
  161.             printf("Get rid of your TSR, pal!\n");
  162.             printf("(Press any key)\n");
  163.             getch();
  164.  
  165.             break;
  166.         }
  167.         case dws_X_BADINPUT:
  168.         {
  169.             /*
  170.              . The mixer funtions can only accept volumes between 0 & 255,
  171.              . the volume will remain unchanged.
  172.             */
  173.             printf("Bad mixer level\n");
  174.  
  175.             break;
  176.         }
  177.         case dws_D_NOTADWD:
  178.         {
  179.             /* You passed the STK a pointer to something which is not a .DWD file! */
  180.             printf("The file you are attempting to play is not a .DWD\n");
  181.  
  182.             break;
  183.         }
  184.         case dws_D_NOTSUPPORTEDVER:
  185.         {
  186.             /*
  187.              . The STK can't play a .DWD converted using a version of VOC2DWD.EXE
  188.              . newer than itself.  And, although we'll try to maintain backwards
  189.              . compatibility, we may not be able to guarantee that newer versions
  190.              . of the code will be able to play older .DWD files.  In any event,
  191.              . it's a good idea to always convert .VOC files with the utility
  192.              . which comes with the library you're linking into your application.
  193.             */
  194.             printf("Please reconvert this file using the VOC2DWD.EXE which came with this library");
  195.  
  196.             break;
  197.         }
  198.         case dws_D_INTERNALERROR:
  199.         {
  200.             /*
  201.              . This error should never occur and probably will not affect sound
  202.              . play(?).  If it happens please contact DiamondWare.
  203.             */
  204.             printf("An internal error has occured\nPlease contact DiamondWare\n");
  205.  
  206.             break;
  207.         }
  208.         case dws_DPlay_NOSPACEFORSOUND:
  209.         {
  210.             /*
  211.              . This error is more like a warning, though it may happen on a
  212.              . regular basis, depending on how many sounds you told the STK
  213.              . to allow in dws_Init, how you chose to prioritize sounds and
  214.              . how many sounds are currently being played.
  215.             */
  216.             printf("No more room for new digitized sounds right now\n");
  217.  
  218.             break;
  219.         }
  220.         case dws_DSetRate_FREQTOLOW:
  221.         {
  222.             /*
  223.              . The STK will set rate as close as possible to the indicated rate
  224.              . but cannot set a rate that low.
  225.             */
  226.             printf("Playback frequency too low\n");
  227.  
  228.             break;
  229.         }
  230.         case dws_DSetRate_FREQTOHIGH:
  231.         {
  232.             /*
  233.              . The STK will set rate as close as possible to the indicated rate
  234.              . but cannot set a rate that high.
  235.             */
  236.             printf("Playback frequency too high\n");
  237.  
  238.             break;
  239.         }
  240.         case dws_MPlay_NOTADWM:
  241.         {
  242.             /* You passed the STK a pointer to something which is not a .DWM file! */
  243.             printf("The file you are attempting to play is not a .DWM\n");
  244.  
  245.             break;
  246.         }
  247.         case dws_MPlay_NOTSUPPORTEDVER:
  248.         {
  249.             /*
  250.              . The STK can't play a .DWM converted using a version of VOC2DWM.EXE
  251.              . newer than itself.  And, although we'll try to maintain backwards
  252.              . compatibility, we may not be able to guarantee that newer versions
  253.              . of the code will be able to play older .DWM files.  In any event,
  254.              . it's a good idea to always convert .MID files with the utility
  255.              . which comes with the library you're linking into your application.
  256.             */
  257.             printf("Please reconvert this file using the MID2DWM.EXE which came with this library\n");
  258.  
  259.             break;
  260.         }
  261.         case dws_MPlay_INTERNALERROR:
  262.         {
  263.             /*
  264.              . This error should never occur and probably will not affect sound
  265.              . play(?).  If it happens please contact DiamondWare.
  266.             */
  267.             printf("An internal error has occured\nPlease contact DiamondWare\n");
  268.  
  269.             break;
  270.         }
  271.         default:
  272.         {
  273.             /*
  274.              . This should never occur and probably will not affect sound
  275.              . play(?). If it happens please contact DiamondWare.
  276.             */
  277.             printf("I'm confused!  Where am I?  HOW DID I GET HERE????\n");
  278.             printf("The ERROR number is: %d\n",errornum);
  279.         }
  280.     }
  281. }
  282.  
  283.  
  284. void main(int argc, char **argv)
  285. {
  286.     FILE                                *fp;
  287.     dws_DETECTOVERRIDES dov;
  288.     dws_DETECTRESULTS     dres;
  289.     dws_IDEAL                     ideal;
  290.     dws_DPLAY                     dplay;
  291.     word                                result;
  292.  
  293.     printf("\nPLAYDWD is Copyright 1994 DiamondWare, Ltd.\nAll rights reserved.\n\n\n");
  294.  
  295.     if (argc < 2)
  296.     {
  297.         printf("Usage PLAYDWD <dwd-file> \n");
  298.         exit(-1);
  299.     }
  300.  
  301.     fp = fopen(argv[1], "rb");
  302.  
  303.     if (fp == NULL)
  304.     {
  305.         printf("Unable to open %s\n", argv[1]);
  306.         exit(-1);
  307.     }
  308.  
  309.     fread(sound, (size_t)BUFFSIZE, 1, fp);    //if file's len < BUFFSIZE, this works
  310.  
  311.     fclose(fp);
  312.  
  313.     /*
  314.      . We need to set every field to -1 in dws_DETECTOVERRIDES struct; this
  315.      . tells the STK to autodetect everything.    Any other value
  316.      . overrides the autodetect routine, and will be accepted on
  317.      . faith, though the STK will verify it if possible.
  318.     */
  319.     dov.baseport = (word)-1;
  320.     dov.digdma     = (word)-1;
  321.     dov.digirq     = (word)-1;
  322.  
  323.     if (!dws_DetectHardWare(&dov, &dres))
  324.     {
  325.         DisplayError(dws_ErrNo());
  326.     }
  327.  
  328.     if (!(dres.capability & dws_capability_DIG))
  329.     {
  330.         printf("DIG support not found\n");
  331.         exit(-1);
  332.     }
  333.  
  334.     /*
  335.      . The "ideal" struct tells the STK how you'd like it to initialize the
  336.      . sound hardware.    In all cases, if the hardware won't support your
  337.      . request, the STK will go as close as possible.  For example, not all
  338.      . sound boards will support al sampling rates (some only support 5 or
  339.      . 6 discrete rates).
  340.     */
  341.     ideal.musictyp     = 0;             //0=No music, 1=OPL2
  342.     ideal.digtyp         = 8;             //0=No Dig, 8=8bit
  343.     ideal.digrate      = 5000;        //sampling rate, in Hz
  344.                                                             //we could have called dws_DGetRateFromDWD
  345.                                                             //before initing the STK to get the correct rate
  346.     ideal.dignvoices = 16;            //number of voices (up to 16)
  347.     ideal.dignchan     = 1;             //1=mono
  348.  
  349.     if (!dws_Init(&dres, &ideal))
  350.     {
  351.         DisplayError(dws_ErrNo());
  352.         exit(-1);
  353.     }
  354.  
  355.     /*
  356.      . Set Master Volume to about 4/5 th's of max
  357.     */
  358.     if (!dws_XMaster(200))
  359.     {
  360.         DisplayError(dws_ErrNo());
  361.     }
  362.  
  363.     dplay.snd          = sound;
  364.     dplay.count      = 1;                 //0=infinite loop, 1-N num times to play sound
  365.     dplay.priority = 1000;
  366.     dplay.presnd     = 0;
  367.  
  368.     if (!dws_DGetRateFromDWD(dplay.snd, &ideal.digrate))
  369.     {
  370.         DisplayError(dws_ErrNo());
  371.         goto KillIt;
  372.     }
  373.  
  374.     if (!dws_DSetRate(ideal.digrate))
  375.     {
  376.         DisplayError(dws_ErrNo());
  377.         goto KillIt;
  378.     }
  379.  
  380.     if (!dws_DPlay(&dplay))
  381.     {
  382.         DisplayError(dws_ErrNo());
  383.         goto KillIt;
  384.     }
  385.  
  386.     do
  387.     {
  388.         if (!dws_DSoundStatus(dplay.soundnum, &result))
  389.         {
  390.             DisplayError(dws_ErrNo());
  391.             goto KillIt;
  392.         }
  393.  
  394.     } while (result);
  395.  
  396.     KillIt:
  397.  
  398.     if (!dws_Kill())
  399.     {
  400.         /*
  401.          . If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  402.          . or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  403.          . must remove his tsr, and dws_Kill must be called again.    If it's
  404.          . dws_NOTINITTED, there's nothing to worry about at this point.
  405.         */
  406.         DisplayError(dws_ErrNo());
  407.  
  408.         if (dws_ErrNo() == dws_Kill_CANTUNHOOKISR)
  409.         {
  410.             goto KillIt;
  411.         }
  412.     }
  413. }
  414.